package net.huashitong.supervision.task.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.DeptService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.insideDuCha.filter.InsideSubTaskFilter;
import net.huashitong.supervision.insideDuCha.filter.InsideUserTaskFilter;
import net.huashitong.supervision.insideDuCha.service.InsideUserTaskService;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.filter.TaskSubFilter;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.service.TaskLogNewService;
import net.huashitong.supervision.task.service.TaskMainService;
import net.huashitong.supervision.task.service.TaskNewListService;
import net.huashitong.supervision.task.service.TaskUnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 督查列表功能 新
 * 按未签收 ，已反馈，未反馈三个菜单的列表
 */
@Controller
@RequestMapping(value = "/tasklist")
public class TaskNewListCtrl  extends JsonBaseController {


    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private SysLoginService sysLoginService;



    @Autowired
    private TaskNewListService taskNewListService;

    @Override
    protected String getView(String s) {
        return "/modules/task/newTaskList/"+s;
    }

    /**
     * 未签收
     * @param model
     * @return
     */
    @RequestMapping(value = "/normalList", method = RequestMethod.GET)
    public String getTaskList(Model model)
    {
        return getView("unSign");
    }

    /**
     * 未反馈页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/unBackList", method = RequestMethod.GET)
    public String unBackList(Model model)
    {
        return getView("unbackView");
    }

    /**
     * 已反馈
     * @param model
     * @return
     */
    @RequestMapping(value = "/backedList", method = RequestMethod.GET)
    public String backedList(Model model)
    {
        return getView("backedList");
    }


    /**
     * 普通任务查询 和重大事项 以及攻坚任务的查询入口
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/normalList", method = RequestMethod.POST)
    public JqGrid normalList(TaskUnitFilter filter) {

        /**
         * 01  县领导
         * 02  主要领导
         * 03  分管领导
         * 04  经办人
         */

        String roleId = SysUserUtils.getRoleId();
        String loginId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();

        if(StringUtils.isBlank(loginId))
        {
            return coverJqGrid(filter ,null);

        }
        if("04".equals(roleId)){
            filter.setUserId(loginId);

        }else if("02".equals(roleId)) {
            filter.setUserId(loginId);
            filter.setCreateUser(loginId);
            filter.setDeptId(deptId);
        }else {
            filter.setDeptId(null);
            filter.setUserId(loginId);
        }
        List<TaskUnit> normalTaskList = taskNewListService.getNormalTaskList(filter);
        return coverJqGrid(filter ,normalTaskList);
    }




    /**
     * 经办已签收任务
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/operaList", method = RequestMethod.POST)
    public JqGrid jingbanListData(TaskUnitFilter filter) {
       // filter.setUnitType("1");
       // filter.setImp(0);
       /* String userId = SysUserUtils.getUserId();
        SysUser userBean = sysUserService.getById(userId);
        String personSignal = userBean.getPersonSignal();
        if("1".equals(personSignal)){//公共账号
			 String roleName="01".equals(roleId)?"县领导":"02".equals(roleId)?"主要领导":"03".equals(roleId)?"分管领导":"04".equals(roleId)?"经办人":"未知";

        }*/
        /**
         * 01  县领导
         * 02  主要领导
         * 03  分管领导
         * 04  经办人
         */

        String roleId = SysUserUtils.getRoleId();
        String loginId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();

        if(StringUtils.isBlank(loginId))
        {
            return coverJqGrid(filter ,null);

        }
        if("04".equals(roleId)){
            filter.setUserId(loginId);

        }else if("02".equals(roleId)) {
            filter.setUserId(loginId);
            filter.setCreateUser(loginId);
            filter.setDeptId(deptId);
        }else {
            filter.setDeptId(null);
            filter.setUserId(loginId);
        }
        List<TaskUnit> normalTaskList = taskNewListService.getOperaTaskList(filter);
        return coverJqGrid(filter ,normalTaskList);
    }


    /**
     * 内部督查任务
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insideList", method = RequestMethod.POST)
    public JqGrid insideList(InsideSubTaskFilter filter) {
        /**
         * 01  县领导
         * 02  主要领导
         * 03  分管领导
         * 04  经办人
         */



        List<InsideSubTaskFilter> insideTask = taskNewListService.getInsideTask(filter);

        return coverJqGrid(filter ,insideTask);
    }


    /**
     * 批量批转
     */
    @RequestMapping(value = "/showBatchTurn/{ids}", method = RequestMethod.GET)
    public String batchTurn(@PathVariable List<String> ids,Model model, InsideUserTaskFilter filter)
    {
        System.out.println(ids);
        filter.setIds(ids);
        model.addAttribute("sysMsg",filter);

        return getView("batchTurn");
    }

    /**
     * 批量批转
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchTurn")
    public Message batchTurn(TaskUnitFilter filter) {
//        System.out.println(filter.getSysId()+"  :2222222");
        if(ObjectUtils.isBlank(filter.getIds())||StringUtils.isBlank(filter.getTaskUsers()))
        {
            return coverMessage("500","taskUnitId和taskUsers不能为空");
        }
        List<String> ids = filter.getIds();
        String mess="";
        int i=0;
        for (String id : ids) {
            TaskUnit taskUnit = taskUnitService.getById(id);
            if(ObjectUtils.isBlank(taskUnit))
            {
                i=i+1;
                System.out.println(id+" :失败");
                continue;
            }

            TaskUnitFilter upfilter = new TaskUnitFilter();
            upfilter.setSysId(id);
            upfilter.setTaskSubId(taskUnit.getTaskSubId());
            //upfilter.setUserId(filter.getTaskUsers());
            upfilter.setTaskUsers(filter.getTaskUsers());
            String s = taskUnitService.saveTaskUser(upfilter);
            if(!"200".equals(s))
            {
                i=i+1;
                System.out.println(id+"  :失败");
                continue;
            }
            taskUnit.setType("1");
            taskUnitService.update(taskUnit);
        }

        int suss= ids.size()-i;
        mess="操作成功，一共选择"+ids.size()+"条,成功批转"+suss+"条,失败"+i+"条。";

        return coverMessage("200",mess);
    }


    /**
     * 批量签收
     * @param ids  单位任务id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchSign/{ids}")
    public Message batchSign(@PathVariable List<String> ids) {

        String userId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();
        for (String id : ids) {

            TaskUnit taskUnit = taskUnitService.getById(id);
            //如果当前任务是待签收，查看时将任务更新为 待反馈   /任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收)
            if ("0".equals(taskUnit.getType())&&(taskUnit.getDeptId().equals(deptId))) {
                taskUnit.setType("1");
                taskUnit.setUserId(userId);
                taskUnitService.update(taskUnit);
            }
        }



        System.out.println(ids);
        return coverMessage("200");

    }


    /**
     * 根据当前用户锁在单位获取批转人
     */

    @ResponseBody
    @RequestMapping(value = "/endorseList", method = RequestMethod.GET)
    public Message endorseList() {
        /*
         * 01  县领导
         * 02  主要领导
         * 03  分管领导
         * 04  经办人
         */
        List<SysUser> userArrayList =new ArrayList<>();

        String deptId = SysUserUtils.getDeptId();
        if(StringUtils.isBlank(deptId))
        {
            deptId="20190718100000000064";
           // return  coverMessage("500","登陆超时",userArrayList);
        }
        List<SysUserDataBean> loginList = sysLoginService.getByDeptId(deptId);
        List<SysUserDataBean> collect = loginList.stream().filter(obj -> obj.getRoleId().equals("04")).collect(Collectors.toList());
        for (SysUserDataBean item : collect) {
            SysUser user = sysUserService.getById(item.getUserId());
            if(ObjectUtils.isBlank(user))
            {
                continue;
            }
            userArrayList.add(user);
        }

        return coverMessage("200","批转人获取成功",userArrayList);
    }

}
